package com.ds.lens.flink.monitor.common.bo;

import com.ds.lens.flink.monitor.common.BuilderUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Getter
@ToString
public class MetricBundle implements Serializable {
    private static final long serialVersionUID = -6166507730080745386L;
    private String uid;
    private MonitorState state;
    @Setter
    private Map<String, Metric> metrics;
    @Setter
    private long startTime;
    @Setter
    private long endTime;

    @Setter
    private long windowStartTime;

    @Setter
    private long windowEndTime;

    public MetricBundle(MonitorState state) {
        this.uid = UUID.randomUUID().toString();
        this.state = state;
        this.metrics = new HashMap<>();
    }

    public void addMetric(Metric metric) {
        String key = BuilderUtil.generateKey(metric);
        Metric old = metrics.get(key);
        if (old != null) {
            old.getValues().addAll(metric.getValues());
            metric = old;
        }
        metrics.put(key, metric);
    }

}
